package regular.com.baidu;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author fanhongxing
 * @desc 常用分组语句
 * @desc http://deerchao.net/tutorials/regex/regex-1.htm
 */
public class Pattern_1 {

	private static final String S_1 = "fan.*?(?=xing)"; // 匹配fanhong
	private static final Pattern P_1 = Pattern.compile(S_1, Pattern.DOTALL);

	private static final String S_2 = "fan.*?hong(?!xing)"; // hong必须是确定字符
	private static final Pattern P_2 = Pattern.compile(S_2, Pattern.DOTALL);

	private static final String S_3 = "(?<=fan).*?$"; // 前面等于fan的后面的字符
	private static final Pattern P_3 = Pattern.compile(S_3, Pattern.DOTALL);

	private static final String S_4 = "(?<!fan)hong.*?$"; // 前面不等于fan的后面的字符，hong是确定字符
	private static final Pattern P_4 = Pattern.compile(S_4, Pattern.DOTALL);

	public static void main(String[] args) {
		String str = "ii_fanhongxing";
		Matcher matcher = P_1.matcher(str);
		if (matcher.find()) {
			System.out.println(matcher.group());
		}
		matcher = P_2.matcher(str);
		if (!matcher.find())
			System.out.println("No Matched!");
		matcher = P_3.matcher(str);
		if (matcher.find())
			System.out.println(matcher.group());
		matcher = P_4.matcher(str);
		if (!matcher.find())
			System.out.println("No Matched!");
	}
}
